<?php
/*
 * 关注
 */
class App_Follow extends Data_Item 
{
    private $_type, $_nid;
    private $_uid;

    public function __construct ($uid = NULL)
    {
        parent::__construct();
        $this->_commTableName = 'app_follow';
        $this->_uid = (int) $uid;
    }

    public function setGroupType ($type, $nid = NULL)
    {
        $this->_type = $type;
        $this->_nid  = $nid;
        return $this;
    }

    public function countsFollow()
    {
        return $this->counts(array(
            array('uid', '=', $this->_uid),
            array('type', '=', $this->_type)));
    }

    public function getFollowUsers($include_followed_status = TRUE)
    {
        $records = $this->items(array(
            array('uid', '=', $this->_uid),
            array('type', '=', $this->_type)), array('created', 'desc'), NULL, NULL);
        

        $users = array();
        foreach ($records as $rs)
        {
            $uid = $rs['nid'];
            if (! array_key_exists($uid, $users))
            {
                $user = User::getUserFromUid($uid);
                $users[$uid] = $user->getUserInfo();

           }
        }

        if ($include_followed_status)
        {
            $followedUsers = $this->getFollowedUsers(FALSE);

            foreach ($users as $uid => & $u)
            // 对方已关注我
            if (array_key_exists($uid, $followedUsers))
                $u['followed'] = TRUE;
            else
                $u['followed'] = FALSE;
        }

        return $users;
    }

    public function countsFollowed()
    {
        return $this->counts(array(
            array('nid', '=', $this->_uid),
            array('type', '=', $this->_type)));
    }

    public function getFollowedUsers($include_follow_status = TRUE)
    {
        $records = $this->items(array(
            array('nid', '=', $this->_uid),
            array('type', '=', $this->_type)), array('created', 'desc'), NULL, NULL);
        

        $users = array();
        foreach ($records as $rs)
        {
            $uid = $rs['uid'];
            if (! array_key_exists($uid, $users))
            {
                $user = User::getUserFromUid($uid);
                $users[$uid] = $user->getUserInfo();

           }
        }

        if ($include_follow_status)
        {
	        $followUsers = $this->getFollowUsers(FALSE);
	        foreach ($users as $uid => & $u)
	        {
	            // 我已关注对方
	            if (array_key_exists($uid, $followUsers))
	                $u['follow'] = TRUE;
	            else
	                $u['follow'] = FALSE;
	        }
        }
         
        return $users;
    }

    public function followUser ($type, $userUid)
    {
        Kohana::$log->add(Log::DEBUG, "followUser: $type, $userUid, " . $this->_uid);
        if ($type == 'del')
        {
            $this->delete(array(
                array('type', '=', 'user'),
                array('nid',  '=', $userUid),
                array('uid',  '=', $this->_uid)));
        }
        else if ($type == 'add')
        {
            if ($this->counts(array(
                array('type', '=', 'user'),
                array('nid',  '=', $userUid),
                array('uid',  '=', $this->_uid))) > 0)
            {
                // 已添加
            }
            else
            {
                $row = array(
                    'type' => 'user',
                    'uid'  => $this->_uid,
                    'nid'  => $userUid,
                    'created' => DB::expr('unix_timestamp(now())'),
                );

                $this->insert($row);
            }
        }
    }

    public function hasFollowUser($uid)
    {
        $where = array(
            array('nid', '=', $uid),
            array('uid', '=', $this->_uid),
            array('type', '=', $this->_type));

        return $this->counts($where);
    }

    public function getNids ($start, $limit)
    {
        $items = $this->items(array(
            array('action', '=', 'like'),
            array('type', '=', $this->_type),
            array('uid', '=', $this->_uid)
            ), array('created', 'desc'), $start, $limit);

        $nids = array();
        foreach ($items as $it)
        {
            $nids[] = $it['nid'];
        }

        return $nids;
    }
}
